﻿using System;
using System.Data;
using System.Windows.Forms;
using Saturn.App.FormOper;
using Saturn.App.Common;

namespace Saturn.App
{
    public partial class FormBillAdd : Form
    {
        int pageIndex = 1;
        int count = 0;
        int pageSize = 6;
        int pageCount = 1;

        SaturnWebReference.SaturnWebService soap = new Saturn.App.SaturnWebReference.SaturnWebService();
        public FormBillAdd()
        {
            InitializeComponent();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (pageIndex == pageCount || pageCount == 0)
            {
                CommonSound.MessageBeep(CommonSound.Information);
                MessageBox.Show("当前是最后一页！", "Error");
                return;
            }
            else
            {
                pageIndex++;
                DataBind();
            }
        }

        private void btnPre_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(cmb_billcode.Text))
            {
                MessageBox.Show("请选择生产订单号");
                return;
            }
            if (String.IsNullOrEmpty(txt_entrycode.Text))
            {
                MessageBox.Show("请扫描入库凭证");
                this.txt_entrycode.Focus();
                return;
            }
            if (String.IsNullOrEmpty(txt_productbar.Text))
            {
                MessageBox.Show("请扫描商品条形码");
                this.txt_productbar.Focus();
                return;
            }
            if (String.IsNullOrEmpty(txt_productnum.Text))
            {
                MessageBox.Show("请输入辅料数量");
                this.txt_productnum.Focus();
                return;
            }
            if (!ClsCommon.IsRegex(txt_productnum.Text))
            {
                CommonSound.MessageBeep(CommonSound.Error);
                MessageBox.Show("数量格式不对，请输入正确的数字！", "Error");
                this.txt_productnum.Text = "";
                this.txt_productnum.Focus();
                return;
            }
            //调用接口添加原辅料订单
            bool res= soap.AddBillMain(cmb_billcode.Text,txt_entrycode.Text,CommonCfg.Userrealname,DateTime.Now.ToString(),txt_productnum.Text,this.com_material_type.SelectedValue.ToString());
            if (res)
            {
                MessageBox.Show("已成功添加原辅料订单");
            }
            else
            {
                MessageBox.Show("添加失败");
            }

        }


        private void FrmReciveList_Load(object sender, EventArgs e)
        {
            this.txt_entrycode.Focus();
            DataBind();
        }
        private void DataBind()
        {
            DataSet ds= soap.GetBillcode();
            cmb_billcode.DataSource = ds.Tables[0];
            cmb_billcode.DisplayMember = "billcode";

            GetMateriaType(); //自动加载领料类型
        } 
        private void btnReturn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cmb_billcode_SelectedIndexChanged(object sender, EventArgs e)
        {
            lbx_Bom.DataSource = soap.GetBomList(cmb_billcode.Text).Tables[0];
            lbx_Bom.DisplayMember = "AccessName";
        }

        private void GetMateriaType()
        {
            try
            {
                //获得类型列表
                DataSet ds = soap.GetFeedingPosition();
                DataTable dt = ds.Tables[0];

                //将类型值绑定到下拉框中，value值为类型code
                foreach (DataRow dr in dt.Rows)
                {
                    this.com_material_type.DataSource = dt;
                    this.com_material_type.DisplayMember = "ExcipientName";
                    this.com_material_type.ValueMember = "Excipientcode";
                }

                this.com_material_type.SelectedIndex = 0;

            }
            catch (Exception ex)
            {
                MessageBox.Show("操作过程中出现故障，请联系管理人员！\n" + ex.ToString());
                return;
            }

        }
    }
}